//


#import "AM_HomeAddTableViewCell.h"


@implementation AM_HomeAddTableViewCell

// 初始化cell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createTableViewCell];
    }
    return self;
}

// 初始化
-(void)createTableViewCell {
    //
    self.nameLabel = ({
        UILabel *label = [[UILabel alloc]init];
        label.textColor = RGB(125, 125, 125);
        label.font = FontPFRegular(kRelativeWidth(16));
        label.text = @"Name：";
        [self.contentView addSubview:label];
        label;
    });
    //
    self.nameField = ({
        UITextField *textField = [[UITextField alloc]init];
        textField.textColor = RGB(60, 60, 60);
        textField.font = FontPFRegular(kRelativeWidth(15));
        textField.placeholder = @"input a name";
        [self.contentView addSubview:textField];
        textField;
    });
    //
    self.accLabel = ({
        UILabel *label = [[UILabel alloc]init];
        label.textColor = RGB(125, 125, 125);;
        label.font = FontPFRegular(kRelativeWidth(16));
        label.text = @"Account：";
        [self.contentView addSubview:label];
        label;
    });
    //
    self.accField = ({
        UITextField *textField = [[UITextField alloc]init];
        textField.textColor = RGB(60, 60, 60);
        textField.font = FontPFRegular(kRelativeWidth(15));
        textField.placeholder = @"input a account";
        [self.contentView addSubview:textField];
        textField;
    });
    //
    self.pwLabel = ({
        UILabel *label = [[UILabel alloc]init];
        label.textColor = RGB(125, 125, 125);
        label.font = FontPFRegular(kRelativeWidth(16));
        label.text = @"Password：";
        [self.contentView addSubview:label];
        label;
    });
    //
    self.pwField = ({
        UITextField *textField = [[UITextField alloc]init];
        textField.textColor = RGB(60, 60, 60);
        textField.font = FontPFRegular(kRelativeWidth(15));
        textField.placeholder = @"input a password";
        [self.contentView addSubview:textField];
        textField;
    });
    //
    self.actionButton = ({
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:@"UploadPictures" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:FontPFRegular(kRelativeWidth(13))];
        [button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:RGB(222, 152, 84)];
        [button am_rounded:5];
        [self.contentView addSubview:button];
        button;
    });
    //
    self.IMG = ({
        UIImageView *img = [[UIImageView alloc]init];
        [self.contentView addSubview:img];
        img;
    });
    
}

- (void)setNeedsLayout {
    [super setNeedsLayout];
    //
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(kRelativeHeight(15));
        make.left.mas_offset(kRelativeWidth(12));
        make.width.mas_equalTo(kRelativeWidth(100));
        make.height.mas_equalTo(kRelativeHeight(20));
    }];
    //
    [self.nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(kRelativeWidth(20));
        make.width.mas_equalTo(kRelativeWidth(200));
        make.height.mas_equalTo(kRelativeHeight(30));
    }];
    //
    [self.accLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(kRelativeHeight(15));
        make.left.mas_offset(kRelativeWidth(12));
        make.width.mas_equalTo(kRelativeWidth(100));
        make.height.mas_equalTo(kRelativeHeight(20));
    }];
    //
    [self.accField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.accLabel);
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(kRelativeWidth(20));
        make.width.mas_equalTo(kRelativeWidth(200));
        make.height.mas_equalTo(kRelativeHeight(30));
    }];
    //.
    [self.pwLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.accLabel.mas_bottom).offset(kRelativeHeight(15));
        make.left.mas_offset(kRelativeWidth(12));
        make.width.mas_equalTo(kRelativeWidth(100));
        make.height.mas_equalTo(kRelativeHeight(20));
    }];
    //
    [self.pwField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pwLabel);
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(kRelativeWidth(20));
        make.width.mas_equalTo(kRelativeWidth(200));
        make.height.mas_equalTo(kRelativeHeight(30));
    }];
    //
    [self.actionButton  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pwLabel.mas_bottom).offset(kRelativeHeight(20));
        make.left.mas_offset(kRelativeWidth(12));
        make.width.mas_equalTo(kRelativeWidth(100));
        make.height.mas_equalTo(kRelativeHeight(35));
    }];
    //
    [self.IMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.actionButton.mas_bottom).offset(kRelativeHeight(10));
        make.left.mas_offset(kRelativeWidth(12));
        make.width.mas_equalTo(kRelativeWidth(50));
        make.height.mas_equalTo(kRelativeHeight(50));
    }];
    
}

//MARK:点击
- (void)action {
    //
    if (self.uploadBlock) {
        self.uploadBlock();
    }
}

- (void)setModel:(HomeData *)model {
    _model = model;
    
    self.nameField.text = model.name;
    self.accField.text = model.account;
    self.pwField.text = model.password;
    
    //
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 读取沙盒路径图片
    NSString *imageFilePath = [path stringByAppendingPathComponent:model.img ];
    UIImage *imgFromUrl=[[UIImage alloc]initWithContentsOfFile:imageFilePath];
    
    self.IMG.image = imgFromUrl;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
